package org.software.views.classes;

import org.software.controller.ClassController;
import org.software.views.MenuFrame;

import javax.swing.*;
import java.awt.*;

public class ClassFrame extends JFrame {
    public ClassFrame() {
        setTitle("新增班级");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);


        JLabel classlabel = new JLabel("班级名称");
        JTextField classfield = new JTextField(15);
        JLabel resultLabel = new JLabel("",JLabel.CENTER);
        resultLabel.setForeground(Color.red);
        JButton addButton = new JButton("新增");
        JButton backButton = new JButton("返回");

        setLayout(new FlowLayout(FlowLayout.CENTER,30,10));
        add(classlabel);
        add(classfield);
        add(resultLabel);

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(addButton);
        buttonPanel.add(backButton);
        add(buttonPanel);

        addButton.addActionListener(e -> {
            String className = classfield.getText().trim();
            if (className.isEmpty()) {
                JOptionPane.showMessageDialog(ClassFrame.this, "班级名称不能为空！", "警告", JOptionPane.WARNING_MESSAGE);
            }else {
                ClassController classController = new ClassController();
                boolean flag=classController.classAdd(className);
                if (flag) {
                    JOptionPane.showMessageDialog(ClassFrame.this,"添加成功！");
                }

            }
        });
        buttonPanel.add(backButton);
        buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER,30,10));
        backButton.addActionListener(e -> {
            dispose();
            new MenuFrame();
        });


        setVisible(true);
    }
}
